package cn.goodym.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.goodym.po.CaseItem;
import cn.goodym.po.CasePlan;
import cn.goodym.po.CasePort;
import cn.goodym.po.CasesTest;
import cn.goodym.po.Env;
import cn.goodym.po.JsonVar;
import cn.goodym.po.PagingVO;
import cn.goodym.service.CaseItemService;
import cn.goodym.service.CasePlanService;
import cn.goodym.service.CasePortService;
import cn.goodym.service.CasesTestService;
import cn.goodym.service.EnvService;
import cn.goodym.util.GetMethod;
import cn.goodym.util.JsonTools;
import cn.goodym.util.PostMethod;

/**
 * 测试计划管理层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/plan")
public class PlanController {
	
	@Resource
	private CaseItemService caseItemService;
	@Resource
	private EnvService envService;
	@Resource
	private CasePlanService casePlanService;
	@Resource
	private CasePortService casePortService;
	@Resource
	private CasesTestService casesTestService;

	/**
	 * 测试计划列表
	 * @param model
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/showPlan")

	public String showPlan(Model model, Integer page) throws Exception {
		List<CasePlan> list = null;
		PagingVO pagingVO = new PagingVO();
		// 设置总页数
		pagingVO.setTotalCount(casePlanService.getCount());
		if (page == null || page == 0) {
			pagingVO.setToPageNo(1);
			list = casePlanService.findByPaging(1);
		} else {
			pagingVO.setToPageNo(page);
			list = casePlanService.findByPaging(page);
		}
		model.addAttribute("casesPlanList", list);
		model.addAttribute("pagingVO", pagingVO);

		return "plan/showPlan";
	}
	
	/**
	 * 跳转到添加测试计划页面
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addPlan", method = { RequestMethod.GET })
	public String addPlanUI(Model model) throws Exception {
		List<CaseItem> caseItemList = caseItemService.findAll();
		model.addAttribute("caseItemList",caseItemList);
		
		List<Env> envList = envService.findAll();
		model.addAttribute("envList", envList);

		return "plan/addPlan";
	}
	
	/**
	 * 保存测试计划
	 * @param casePlan
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addPlan", method = { RequestMethod.POST })
	public String addPlan(CasePlan casePlan, Model model) throws Exception {
		Date date = new Date();
		casePlan.setCreateTime(date);
		Boolean result = casePlanService.save(casePlan);

		if (!result) {
			model.addAttribute("message", "添加失败");
			return "error";
		}
		return "redirect:/plan/showPlan";
	}
	
	/**
	 * 删除测试计划
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/removePlan")
	public String removePlan(Integer id) throws Exception {

		if (id == null) {
			return "plan/showPlan";
		}
		casePlanService.removeById(id);
		return "redirect:/plan/showPlan";
	}
	
	/**
	 * 跳转到测试计划编辑页面
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editPlan", method = { RequestMethod.GET })
	public String editPlanUI(Integer id, Model model) throws Exception {
		if (id == null) {
			return "redirect:/plan/showPlan";
		}
		
		CasePlan cp = casePlanService.findById(id);
		List<CaseItem> caseItemList = caseItemService.findAll();
		model.addAttribute("caseItemList",caseItemList);
		List<Env> envList = envService.findAll();
		model.addAttribute("envList", envList);
		model.addAttribute("casePlan", cp);
		return "plan/editPlan";
	}
	
	/**
	 * 修改测试计划
	 * @param casePlan
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editPlan", method = { RequestMethod.POST })
	public String editPlan(CasePlan casePlan) throws Exception {
		CasePlan cp = casePlanService.findById(casePlan.getId());
		casePlan.setCreateTime(cp.getCreateTime());
		Date date = new Date();
		casePlan.setUpdateTime(date);
		casePlanService.updateById(casePlan);
		return "redirect:/plan/showPlan";
	}
	
	/**
	 * 查询测试计划
	 * @param planName
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/selectPlan", method = { RequestMethod.POST })
	private String selectPlan(String planName, Model model) throws Exception {
		List<CasePlan> casesPlanList = casePlanService.findByName(planName);
		model.addAttribute("casesPlanList", casesPlanList);
		return "plan/showPlan";
	}
	
	@RequestMapping(value = "/runPlan", method = { RequestMethod.GET })
	public String runPlan(Integer id) throws Exception {
		CasePlan casePlan = casePlanService.findById(id);
		
		Env env = envService.findById(Integer.parseInt(casePlan.getEnvId()));
//		String host = env.getEnvAddress();
		CaseItem caseItem = caseItemService.findById(Integer.parseInt(casePlan.getItemId()));
		
		CasePort casePort = casePortService.findByItemId(caseItem.getId());
//		String portUrl = casePort.getPorturl();
		String paramters = JsonTools.getUrlParams(casePort.getParamters());
		
		CasesTest casesTest = casesTestService.findByPortId(casePort.getId());

		
		//		CasePort casePort = casePortService.findById(Integer.parseInt(portId));
//		String paramters = JsonTools.getUrlParams(casePort.getParamters());
		String resultString = "";
		String requestUrl = env.getEnvAddress() + casePort.getPorturl();
		if (Integer.valueOf(casePort.getType()) == 0) {
			resultString = GetMethod.sendGet(requestUrl + paramters,
					"");
		} else if (Integer.valueOf(casePort.getType()) == 1) {
			resultString = PostMethod.sendPost(requestUrl,paramters, "");
		}
		
		
		System.out.println(resultString);
//		JSONObject js = (JSONObject) JSONObject.parse(resultString);
		System.out.println(JsonTools.json2JsonObject(resultString, "status"));

		System.out.println(casesTest.getParamters());
		return "redirect:/plan/showPlan";
	}

	

}
